import 'dart:math';

import 'package:flutter/material.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/routers/jump_router.dart';
import 'package:tiktokadult/tools_base/widget/net_image_widget.dart';

import '../../hj_model/cartoon_media_info.dart';

//acg video item组件
class AcgItemWidget extends StatefulWidget {
  final CartoonMediaInfo? info;
  final bool coverV; //竖屏封面
  final bool? heroUI; //是否需要hero动画
  final Function()? tapCallback;

  const AcgItemWidget({
    Key? key,
    this.info,
    this.coverV = true,
    this.tapCallback,
    this.heroUI = false,
  });

  @override
  State<AcgItemWidget> createState() => _AcgItemWidgetState();
}

class _AcgItemWidgetState extends State<AcgItemWidget> {
  String? heroTag;

  @override
  void initState() {
    if (widget.heroUI == true) {
      heroTag = Random().nextInt(10000).toString();
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: widget.tapCallback ?? () => pushToCartoonPage(widget.info),
      child: Column(
        children: [
          Expanded(
              child: Stack(
            children: [
              LayoutBuilder(
                builder: (BuildContext context, BoxConstraints constraints) {
                  return NetworkImageLoader(
                    imageUrl: widget.coverV ? widget.info?.coverV ?? '' : widget.info?.coverH ?? '',
                    width: constraints.maxWidth,
                    height: constraints.maxHeight,
                    fit: BoxFit.cover,
                  );
                },
              ),
              if (widget.info?.isAudiobooks == true)
                Positioned(
                  left: 8,
                  top: 8,
                  height: 24,
                  width: 24,
                  child: Image.asset('voice_noval_icon.png'.videoPath),
                ),
              Positioned(
                right: 6,
                top: 6,
                child: SymbolWidget(recoFlag: widget.info?.recoFlag ?? 0),
              ),
              Positioned(
                bottom: 0,
                left: 0,
                right: 0,
                child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 6),
                    alignment: Alignment.centerLeft,
                    decoration: BoxDecoration(
                        gradient: LinearGradient(
                      colors: [
                        Colors.black.withOpacity(.0),
                        Colors.black.withOpacity(.6),
                      ],
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter,
                    )),
                    height: 30,
                    child: Row(
                      children: [
                        Image.asset(
                          'acg_count.png'.acgImgPath,
                          width: 16,
                        ),
                        Text(
                          '${widget.info?.countView.humanizeFormat}',
                          style: textStyle(12, Colors.white.withOpacity(.9), FontWeight.w400),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                        Spacer(),
                        Text(
                          '${widget.info?.getUpdateString}',
                          textAlign: TextAlign.left,
                          style: TextStyle(fontSize: 10, color: _statusColor, fontWeight: FontWeight.w400),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ],
                    )),
              )
            ],
          )),
          6.sizeBoxH,
          Row(
            children: [
              Expanded(
                child: Text(
                  widget.info?.title ?? '',
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.white.withOpacity(.9),
                    fontWeight: FontWeight.w400,
                  ),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  textAlign: TextAlign.left,
                ),
              )
            ],
          ),
          4.sizeBoxH,
        ],
      ),
    );
  }

  Color get _statusColor {
    return widget.info?.updateStatus == 1 ? Color(0xffEEC76B) : Colors.white;
  }
}

//符号
class SymbolWidget extends StatelessWidget {
  final int recoFlag; // 推荐标志 1精品 2原创 3专属
  const SymbolWidget({super.key, this.recoFlag = 0});

  @override
  Widget build(BuildContext context) {
    if (recoFlag == 1)
      return Container(
        width: 32,
        height: 16,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(3),
            gradient: LinearGradient(
              colors: [
                Color(0xffF38152),
                Color(0xffF11F1F),
              ],
            )),
        child: Text(
          '精品',
          style: textStyle(10, Colors.white, FontWeight.w400),
        ),
      );
    if (recoFlag == 2)
      return Container(
        width: 32,
        height: 16,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(3),
          color: Color(0xff292929),
          border: Border.all(width: 1, color: Color(0xffFFBB00)),
        ),
        child: Text(
          '原创',
          style: textStyle(10, Color(0xffFFBB00), FontWeight.w400),
        ),
      );
    if (recoFlag == 3)
      return Container(
        width: 32,
        height: 16,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(3),
            gradient: LinearGradient(
              colors: [
                Color(0xffF38152),
                Color(0xffF11F1F),
              ],
            )),
        child: Text(
          '专属',
          style: textStyle(10, Colors.white, FontWeight.w400),
        ),
      );

    return Container();
  }
}
